<template>
  <XtxDialog title="取消订单" v-model:visible="visibleDialog">
    <!-- 组件内容 -->
      <div class="cancel-info">
        <p>取消订单后，本单享有的优惠可能会一并取消，是否继续？</p>
        <p class="tip">请选择取消订单的原因（必选）：</p>
        <div class="btn">
          <a
            @click="curText = item"
            v-for="item in cancelReason"
            :key="item"
            href="javascript:;"
            :class="{ active: curText === item }"
          >
            {{ item }}
          </a>
        </div>
    </div>
    <!-- 按钮操作 -->
    <template #footer>
      <XtxButton type="gray" @click="visibleDialog=false" style="margin-right:20px">取消</XtxButton>
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { markRaw, ref } from 'vue'
import { cancelReason } from '@/api/constains'
import {cancelOrder} from '@/api/order'
import Message from '@/components/library/Message'

export default {
  name: 'OrderCancel',
  setup (props, {emit}) {
    const visibleDialog = ref(false)
    // 打开对话框，提供给其他组件调用
    const open = (order) => {
        visibleDialog.value = true
        currOrder.value = order
    }
    // 订单对象
    const currOrder = ref(null)
    // 选中原因的text
    const curText = ref('')
    // 点击确认
    const submit = () => {
        if(!curText.value){
            Message({text:'请选择原因'})
            return
        }
        cancelOrder({
           orderId: currOrder.value.id,
           cancelReason: curText.value
        }).then(res=>{
             // 修改订单状态
             currOrder.value.state = 6
            emit('cancel-state')
            visibleDialog.value=false
        })
    }
    return { visibleDialog, cancelReason ,curText, open, submit}
  }
}
</script>
<style scoped lang="less">
 .xtx-dialog :deep(.wrapper) {
  width: 620px;
}
.cancel-info {
  p {
    font-size: 16px;
    line-height: 35px;
    &.tip {
      color: #999;
    }
  }
  .btn {
    padding-top: 21px;
    display: flex;
    flex-wrap: wrap;
    a {
      width: 256px;
      height: 45px;
      line-height: 45px;
      text-align: center;
      background-color: #ffffff;
      border: 1px solid #e4e4e4;
      margin-right: 20px;
      margin-bottom: 20px;
      color: #666;
      &:nth-child(2n){
        margin-right: 0;
      }
      &:hover,&.active {
        background-color: #e3f9f4;
        border-color: @xtxColor;
      }
    }
  }
}
</style>